<template>
  <view class="video-play">
    <!-- 背景图片 -->
    <image :src="videoObj.img"></image>

    <!-- 右上角图标 -->
    <view class="video-tool">
      <view :class="['iconfont',muted?'iconjingyin':'iconshengyin']"
        @click="handleMuted"></view>
      <view class="iconfont iconzhuanfa">
        <button open-type="share"></button>
      </view>
    </view>
    <!-- 视频 -->
    <view class="video-wrap">
      <!-- objectFit="Fill" 取消video左右黑色边框 -->
      <video :src="videoObj.video" objectFit="Fill" :muted="muted"></video>
    </view>
    <!-- 下载 -->
    <view class="download">
      <view class="download-btn" @click="handleDownload">下载高清</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 视频数据对象
      videoObj: {},
      // 是否静音
      muted: false
    }
  },
  onLoad(options) {
    this.videoObj = getApp().globalData.video
  },
  methods: {
    // 点击图标切换有声与静音
    handleMuted() {
      this.muted = !this.muted
    },
    // 下载视频
    async handleDownload() {
      uni.showLoading({
        title: '下载中'
      })
      // 1 将远程文件下载到小程序内存中
      // 2 将小程序内存中的临时文件下载到本地上，电脑的话会弹窗询问下载路径，手机的话会自动下载到手机系统的相册
      const res1 = await uni.downloadFile({
        url: this.videoObj.video
      })
      const { tempFilePath } = res1[1]
      const res2 = await uni.saveVideoToPhotosAlbum({
        filePath: tempFilePath
      })
      uni.hideLoading()
      // 成功下载后
      if (res2[1]) {
        uni.showToast({
          title: '下载成功',
          icon: 'none'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.video-play {
  position: relative;
  height: 100vh;
  image {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: -1;
    // CSS3 滤镜 高斯模糊
    filter: blur(20px);
  }

  .video-tool {
    display: flex;
    justify-content: flex-end;
    height: 80rpx;
    margin-top: 10rpx;
    .iconfont {
      width: 80rpx;
      height: 80rpx;
      line-height: 80rpx;
      margin-right: 20rpx;
      text-align: center;
      color: #ffffff;
      font-size: 50rpx;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.2);
    }
    .iconzhuanfa {
      position: relative;
      button {
        position: absolute;
        width: 100%;
        height: 100%;
        opacity: 0;
      }
    }
  }

  .video-wrap {
    display: flex;
    justify-content: center;
    margin-top: 20rpx;
    video {
      width: 360rpx;
      height: 600rpx;
      border-radius: 10rpx;
    }
  }

  .download {
    display: flex;
    justify-content: center;
    margin-top: 30rpx;
    .download-btn {
      width: 360rpx;
      height: 80rpx;
      line-height: 80rpx;
      border-radius: 40rpx;
      border: 3rpx solid #fff;
      text-align: center;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}
</style>